<!DOCTYPE html>
<html>
<head>
    <title>商品管理</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" charset="UTF-8">
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="css/bootstrap-table.min.css"rel="stylesheet" >
    <script type="text/javascript" src="js/jquery.min.js"></script>
    <script type="text/javascript" src="js/bootstrap.min.js"></script>
    <script type="text/javascript" src="js/bootstrap-table.min.js"></script>
    <script type="text/javascript" src="js/bootstrap-table-zh-CN.min.js"></script>
</head>
<body>
<div class="panel panel-default">
    <div class="panel-heading">
        <h3 class="panel-title text-center">商品管理</h3>
    </div>
    <div class="panel-body">

        <div id="toolbar" class="btn-group">
            <button id="btn_buy" type="button" class="btn btn-primary">
                <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>加入购物车
            </button>
            <button id="btn_shopcart" type="button" class="btn btn-primary">
                <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>查看购物车
            </button>
        </div>

        <table data-toggle="table" id="table" data-height="400"
               data-classes="table table-hover" data-striped="true"
               data-pagination="false" data-side-pagination="server"
               data-show-refresh="true" data-show-toggle="true"
               data-show-columns="true" data-toolbar="#toolbar">
            <thead>
            <tr>
                <th data-field="state" data-checkbox='ture'></th>
                <th></th>
                <th></th>
                <th></th>
                <th></th>
            </tr>
            </thead>
        </table>
    </div>
</div>

<!-- 添加商品模态框（Modal） -->
<div class="modal fade" id="saveModal" tabindex="-1" role="dialog"
     aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"
                        aria-hidden="true">×</button>
                <h4 class="modal-title" id="myModalLabel">添加商品信息</h4>
            </div>
            <div class="modal-body">
                <form id="productForm">
                    <input type="hidden" id="id" name="id"/>
                    <div class="form-group">
                        <label>商品名称</label>
                        <input type="text" class="form-control" id="productName" name="productName">
                    </div>
                    <div class="form-group">
                        <label>价格</label>
                        <input type="text" class="form-control" id="price" name="price">
                    </div>
                    <div class="form-group">
                        <label>库存数量</label>
                        <input type="number" class="form-control" id="count" name="count">
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭
                </button>
                <button type="button" class="btn btn-primary" id="saveConfirmBtn">确定</button>
            </div>
        </div>
        <!-- /.modal-content -->
    </div>
    <!-- /.modal-dialog -->
</div>
<!-- /.modal -->
</body>
<script type="text/javascript">
    $(document).ready(function() {
        $("button[name='toggle']").height(20);
        $("button[name='refresh']").height(20);
        $("#saveModal").modal('hide');
        $.ajax({
            url: "checklogin",
            type: 'get',
            dataType: 'json',
            success: function (result){
                if (result.status !== "success") {
                    // 用户未登录，禁用按钮
                    $("#btn_buy").prop('disabled', true)
                        .addClass('btn-secondary')
                        .removeClass('btn-primary');
                    $("#btn_shopcart").prop('disabled', true)
                        .addClass('btn-secondary')
                        .removeClass('btn-primary');
                }
            }

        });
    });

    //添加用户模态框下的确认按钮点击事件
    $("#saveConfirmBtn").click(function() {
        $("#saveModal").modal('hide');
        $.ajax({
            url : "product/save",
            type : 'post',
            dataType:'json',
            data : $("#productForm").serialize(),
            success : function() {
                alert("操作成功！");
                //重新加载表格
                $("#table").bootstrapTable("refresh");
            }
        });
    });

    //添加到购物车按钮点击事件
    $("#btn_buy").click(function() {
        var list = $("table").bootstrapTable('getSelections');
        if (list == null || list.length <= 0) {
            alert("未选中任何项！");
            return;
        }

        var idList = new Array();
        for (var i = 0; i < list.length; i++) {
            idList[i] = list[i].id;
        }
        if (confirm("是否确认加入购物车?")) {
            $.ajax({
                url : "shopcart/add",
                type : 'post',
                dataType : 'json',
                data : {
                    idList : idList
                },
                success : function(data) {
                    if(data.status == "success")
                        alert("商品已添加到购物车");
                    else
                        alert("添加失败！" + data.message);
                }
            });
        }
    });

    $("#btn_shopcart").click(function() {
        window.location.href="shopcart.html"
    });

    $("#table").bootstrapTable({
        url : "product/list",
        clickToSelect : true,
        dataType : "json",
        contentType : "application/x-www-form-urlencoded;charset=utf-8",
        method : 'get',
        responseHandler : function(res) {
            return res;
        },
        columns : [
            {
                field : 'state',
            },
            {
                field : 'id',
                title : 'ID',
                align : 'center'
            },
            {
                field : 'productName',
                title : '产品名称',
                align : 'center'
            },
            {
                field : 'price',
                title : '产品价格',
                align : 'center'
            },
            {
                field : 'count',
                title : '库存数量',
                align : 'center'
            }]
    });
</script>
</html>
